package com.ericsson.rm.controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ericsson.rm.response.ResourceProjectResponse;
import com.ericsson.rm.response.ResourceProjectValueObject;
import com.ericsson.rm.response.RoleDetailResponse;
import com.ericsson.rm.response.RoleDetailValueObject;
import com.ericsson.rm.response.SkillDetailResponse;
import com.ericsson.rm.response.SkillDetailValueObject;
import com.ericsson.rm.service.SkillRoleResourceService;
import com.ericsson.rm.util.Utility;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@Autowired
	SkillRoleResourceService skillRoleResourceService;
		
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String getDataForDropDown(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);

		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG, locale);

		String formattedDate = dateFormat.format(date);

		model.addAttribute("serverTime", formattedDate);
		List<RoleDetailResponse> roleDetail = skillRoleResourceService
				.fetchRoleDetail();
		List<SkillDetailResponse> skillDetail = skillRoleResourceService
				.fetchSkillDetail();
		
		List<RoleDetailValueObject> roleObjectList = new ArrayList<RoleDetailValueObject>(
				roleDetail.size());
		List<SkillDetailValueObject> skillObjectList = new ArrayList<SkillDetailValueObject>(
				skillDetail.size());

	
		String[] ignoredPropertyForRole = new String[10];
		String[] ignoredPropertyForSkill = new String[10];
		for (int i = 0; i < roleDetail.size(); i++) {
			logger.info("roleDetail["+i+"]: "+Utility.displayBean(roleDetail.get(i)));
			
			RoleDetailValueObject roleObject = new RoleDetailValueObject();
			ignoredPropertyForRole[i] = roleDetail.get(i).getGrade();
			
			BeanUtils.copyProperties(roleDetail.get(i), roleObject,	ignoredPropertyForRole);
			logger.info("RoleDetailValueObject: "+Utility.displayBean(roleObject));
			roleObjectList.add(roleObject);
			logger.info("roleObjectList..."+roleObjectList.get(i).getRoleName());
		}

	

		System.out.println("skillDetail size..." + skillDetail.size());		
			for (int i = 0; i < skillDetail.size(); i++) {
				SkillDetailValueObject skillObject = new SkillDetailValueObject();
				ignoredPropertyForRole[i] = skillDetail.get(i).getSkillType();
				logger.info("skillDesc..."+skillDetail.get(i).getSkillDescription());
				BeanUtils.copyProperties(skillDetail.get(i), skillObject,
						ignoredPropertyForSkill);
				skillObjectList.add(skillObject);
				logger.info("skillObjectList..."+skillObjectList.get(i).getSkillDescription());
			}
		
		model.addAttribute("role", roleObjectList);
		model.addAttribute("skill", skillObjectList);

		return "home";
	}

	@RequestMapping(value = "/GetDetail", method = {RequestMethod.GET,RequestMethod.POST})
	public String getSearchResult(
			HttpServletRequest request,
			@RequestParam(required = false, value = "rolename") String jobStage,
			@RequestParam(required = false, value = "skilllist") String skillId,
			HttpServletResponse response, Model model) {

		logger.debug("Enter into getSearchResult method...");
		System.out.println("jobStage is ..."+jobStage);
		List<ResourceProjectResponse> resourceProjectResponseList = new ArrayList<ResourceProjectResponse>();
		List<ResourceProjectValueObject> resourceProjectValueObjectList = new ArrayList<ResourceProjectValueObject>();
			
		resourceProjectResponseList=skillRoleResourceService.fetchResourceDetailWithRoleAndSkill(jobStage,skillId);
		for (int i=0;i<resourceProjectResponseList.size();i++){
			ResourceProjectValueObject resourceProjectValueObject = new ResourceProjectValueObject();
			BeanUtils.copyProperties(resourceProjectResponseList.get(i), resourceProjectValueObject);
			resourceProjectValueObjectList.add(resourceProjectValueObject);
			
		}
			//skillRoleResourceService.fetchRoleIdByRoleName(roleName);
	System.out.println("size of resourceProjectValueObjectList..."+resourceProjectValueObjectList.size());
	
		model.addAttribute("result",resourceProjectValueObjectList);

		return "home";

	}

}
